package com.toune.movie.mvp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.toune.movie.R;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myUtil.ProgressWebview;
import com.toune.movie.mvp.contract.PayContract;
import com.toune.movie.mvp.presenter.PayPresenter;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/17.
 */

public class PayActivity extends BaseActivity<PayPresenter> implements PayContract.View {
    @BindView(R.id.webview)
    ProgressWebview webview;

    @Override // 实例化presenter
    public PayPresenter initPresenter() {
        return new PayPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @Override
    public void init(Bundle savedInstanceState) {
        String url = getIntent().getStringExtra("url");
        int payType = getIntent().getIntExtra("payType", 1);
        MyUtil.showProDialog(context);
        if (payType == 1) {
            //支付宝支付
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.contains("platformapi/startapp")) {
                        startAlipayActivity(url);
                        // android  6.0 两种方式获取intent都可以跳转支付宝成功,7.1测试不成功
                    } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                            && (url.contains("platformapi") && url.contains("startapp"))) {
                        startAlipayActivity(url);
                    } else {
                        view.loadUrl(url);
                    }
                    MyUtil.hidProDialog();
                    return true;
                }
            });
        } else {
            //微信支付
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.startsWith("weixin://wap/pay?")) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                        return true;
                        // android  6.0 两种方式获取intent都可以跳转支付宝成功,7.1测试不成功
                    } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                            && (url.contains("platformapi") && url.contains("startapp"))) {
                        startAlipayActivity(url);
                    } else {
                        view.loadUrl(url);
                    }
                    MyUtil.hidProDialog();
                    return true;
                }
            });
        }

        webview.loadUrl(url);
    }

    // 调起支付宝并跳转到指定页面
    private void startAlipayActivity(String url) {
        Intent intent;
        try {
            intent = Intent.parseUri(url,
                    Intent.URI_INTENT_SCHEME);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setComponent(null);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pay;
    }

    @Override
    protected String getTitleStr() {
        return getString(R.string.pay);
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

//    private class MyWebClient extends WebViewClient {
//        @Override
//        public void onReceivedSslError(WebView view, android.webkit.SslErrorHandler handler, android.net.http.SslError error) {
//            handler.proceed();
//        }
//
//        @Override
//        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
//            // 如下方案可在非微信内部WebView的H5页面中调出微信支付
//            if (url.startsWith("weixin://wap/pay?")) {
//                Intent intent = new Intent();
//                intent.setAction(Intent.ACTION_VIEW);
//                intent.setData(Uri.parse(url));
//                startActivity(intent);
//                return true;
//            } else if (parseScheme(url)) {
//                try {
//                    Intent intent;
//                    intent = Intent.parseUri(url,
//                            Intent.URI_INTENT_SCHEME);
//                    intent.addCategory("android.intent.category.BROWSABLE");
//                    intent.setComponent(null);
//                    // intent.setSelector(null);
//                    startActivity(intent);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            } else {
//                webView.loadUrl(url);
//            }
//            MyUtil.hidProDialog();
//            return true;
//        }
//    }

    public boolean parseScheme(String url) {
        if (url.contains("platformapi/startapp")) {
            return true;
        } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                && (url.contains("platformapi") && url.contains("startapp"))) {
            return true;
        } else {
            return false;
        }
    }
}
